package com.javalaoniu.tool.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * 容量单位转换，B自动转换为KB/MB/GB
 *
 * @author liangxn
 */
public class StorageUnitTransUtil {
    /**
     * 容量单位转换，B自动转换为KB/MB/GB
     *
     * @param size B的大小
     * @return 转换后的容量单位
     */
    public static String byte2Auto(long size) {
        StringBuilder bytes = new StringBuilder();
        DecimalFormat format = new DecimalFormat("###.00");
        if (size >= 1024 * 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0 * 1024.0));
            bytes.append(format.format(i)).append("GB");
        } else if (size >= 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0));
            bytes.append(format.format(i)).append("MB");
        } else if (size >= 1024) {
            double i = (size / (1024.0));
            bytes.append(format.format(i)).append("KB");
        } else {
            if (size <= 0) {
                bytes.append("0B");
            } else {
                bytes.append(size).append("B");
            }
        }
        return bytes.toString();
    }


    public static void main(String[] args) {
        //System.out.println(byte2Auto(1234567845));
        BigDecimal last = new BigDecimal(0);
        BigDecimal preLow = new BigDecimal(0);
        System.out.println(last.subtract(preLow).divide(preLow, 8, RoundingMode.CEILING));

        BigDecimal amount = BigDecimal.ZERO;
        BigDecimal test = new BigDecimal("3.36");
        System.out.println(amount.add(test));

        System.out.println(last.compareTo(preLow));

        last.subtract(preLow).divide(preLow, 8, RoundingMode.CEILING);
    }
}
